﻿/*
 * Сделано в SharpDevelop.
 * Пользователь: Badanin M.V.
 * Дата: 14.05.2013
 * Время: 10:50
 * 
 */
using System;
using System.Xml;
using System.Text;
using System.Collections.Generic;
namespace Region.Utils
{
  /// <summary>
  /// Description of ExportOffice.
  /// </summary>
  public abstract class ExportOffice: Export
  {
    protected Zip m_zip;    
    String m_TypeOfficeDocument;
    static Dictionary<String,String> MainFileName = new Dictionary<String, String>(){{"word","document.xml"},{"xl","workbook.xml"}};
    protected bool AddContentType(String Extension,String ContentType)
    {      
      var CT = new XmlDocument();
      bool Edit = false;
      bool Find = false;       
      CT.LoadXml(Encoding.UTF8.GetString(m_zip.readAll("[Content_Types].xml")));
      foreach (XmlElement d in CT.DocumentElement.GetElementsByTagName("Default"))
        if (d.GetAttribute("Extension").ToUpper()==Extension.ToUpper())
      {
        Find = true;
        if (d.GetAttribute("ContentType").ToUpper()!=ContentType.ToUpper())
        {
          d.SetAttribute("ContentType",ContentType);
          Edit = true;
        }
      }
      if (!Find)
      {
        var El = (XmlElement)CT.DocumentElement.Clone();
        El.InnerXml = "<Default />";
        var Default = (XmlElement)El.FirstChild;        
        Default.SetAttribute("Extension",Extension);
        Default.SetAttribute("ContentType",ContentType);
        CT.DocumentElement.InsertBefore(Default,CT.DocumentElement.FirstChild);
        Edit = true;
      }
      if(Edit) m_zip.writeAll("[Content_Types].xml",Encoding.UTF8.GetBytes(CT.InnerXml));
      return true;
    }
    protected String addDocumentRel(Byte[] Data,String Target,String Type)
    {
      var rel = new XmlDocument();
      rel.LoadXml(Encoding.UTF8.GetString(m_zip.readAll(m_TypeOfficeDocument+@"/_rels/"+MainFileName[m_TypeOfficeDocument]+".rels")));
      String ID = "rId"+(rel.DocumentElement.GetElementsByTagName("Relationship").Count+1);
      var el = rel.DocumentElement.Clone();el.InnerXml = "<Relationship />";
      var Relationship = (XmlElement)el.FirstChild;
      //var Relationship = rel.CreateElement("","Relationship","");
      Relationship.SetAttribute("Target",Target);
      Relationship.SetAttribute("Type",Type);
      Relationship.SetAttribute("Id",ID);            
      rel.DocumentElement.AppendChild(Relationship);      
      m_zip.writeAll(m_TypeOfficeDocument+@"/_rels/"+MainFileName[m_TypeOfficeDocument]+".rels",Encoding.UTF8.GetBytes(rel.InnerXml));      
      m_zip.writeAll(m_TypeOfficeDocument+"/"+Target,Data);
      return ID;
    }    
    public ExportOffice(String TypeOfficeDocument){m_TypeOfficeDocument = TypeOfficeDocument;}
    public virtual bool EditDocument(TaskManager.Task task = null){return false;}
    public override bool Save(TaskManager.Task task = null)
    {
      if (task!=null) task.TaskName = "Чтение шаблона документа";
      byte[] Temp = Utils.Compression.DecodeGZ(Template);
      m_zip = new Zip();
      if (!m_zip.readZIP(new MEMStream(Temp))) return false;
      if (!EditDocument(task)) return false;
      if (task!=null) task.TaskName = "Запись документа на диск";
      return m_zip.writeZIP(OutFile);
    }    
  }
}
